<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content">
    <!--<div class="row">-->
        <!--<div class="col-xs-12">-->
            <!--<div class="right" style="color: #FF0000;">-->
                <!--<h4>创建变更单注意说明:当图纸变更时，制作件清单也要同时变更。变更要求是，将原制作件清单图号删除，同时新增新的图号进行区分。</h4>-->
            <!--</div>-->
        <!--</div>-->
    <!--</div>-->
    <div class="row" style="margin-bottom: 24px">
        <a th:href="@{/production/list}"
           class="btn btn-default pull-right" style="margin-right: 10px">
            <span class="Bold">取消</span>
        </a>
        <a id="btn-save"  th:if="${isChange!=-1}" shiro:hasPermission="production:list:cha" href="javascript:void(0);"
           class="btn btn-success pull-right" style="margin-right: 10px">
            <span class="Bold"> 保存</span>
        </a>
    </div>
    <div class="row">
        <form id="sheetForm" class="form-horizontal">
            <div class="col-xs-11 col-xs-offset-1">
                <input type="hidden" name="id" id="makeSheetId" th:value="${makeSheet.id}">
                <table class="t1 t3">
                    <thead>
                    <tr>
                        <td colspan="14">
                            <img th:src="@{/img/logo3.jpg}" width="64px" height="40px" style="margin-top: -30px">
                            <div style="display: inline-block;margin-top: 5px;margin-left: 20px">
                                <span style="letter-spacing: 24px;display: block;line-height: 20px">&nbsp;辽宁忠旺机械设备制造有限公司</span>
                                <span style="display: block;font-size: 12px;line-height: 20px">Liaoning Zhongwang Machinery Equipment Manufacturing Co., Ltd</span>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="14" style="font-size: 16px;letter-spacing: 4px;">
                            制作件清单变更单
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td colspan="3" style="width: 8%">项目编号</td>
                        <td style="width: 11%;font-size: 14px;">
                            <input id="sheetForm-proCode" type="text" class="form-control" name="proCode"
                                   th:value="${makeSheet.proCode}" readonly>
                        </td>
                        <td style="width: 8%">项目名称</td>
                        <td colspan="3" style="width: 21%">
                            <input id="sheetForm-proName" type="text" class="form-control" name="proName"
                                   th:value="${makeSheet.proName}" readonly>
                        </td>
                        <td style="width: 8%">项目数量</td>
                        <td style="width: 6%">
                            <input id="sheetForm-proNum" type="number" class="form-control" name="proNum"
                                   th:value="${makeSheet.proNum}" readonly>
                        </td>
                        <td style="width: 6%">类型</td>
                        <td style="width: 9%">
                            <input id="sheetForm-malType" type="text" class="form-control" name="special"
                                   th:value="${makeSheet.special}" readonly>
                            <input type="hidden" th:value="${makeSheet.specialId}" name="specialId">
                        </td>
                        <td style="width: 6%">部分</td>
                        <td style="width: 12%">
                            <input id="sheetForm-part" type="text" class="form-control error" name="part"
                                   th:value="${makeSheet.part}" readonly>
                            <input type="hidden" th:value="${makeSheet.partId}" name="partId">
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td style="width: 4%">序号</td>
                        <td colspan="4">图号</td>
                        <td colspan="4">图名</td>
                        <td style="width: 6%">数量</td>
                        <td colspan="2">制作件类型</td>
                        <td colspan="2">备注</td>
                        <td></td>
                    </tr>
                    </thead>
                    <tbody id="out">
                    <tr th:each="plan : ${makePlanORmakeChangeList}" th:class="${planStat.index == 0 ? 'after-tr' : ''}" th:orderNumId="${makeSheet.sheetType == 1 ?plan.resultOrderNum:plan.resultOrderNum}" th:pidNumId="${makeSheet.sheetType == 1?plan.pid:plan.pid}">
                        <input type="hidden" id="tableOrderNum" th:if="${planStat.index == 0}" th:value="${planStat.size}">
                        <td th:text="${planStat.count}"></td>
                        <td colspan="4">
                            <input type="hidden" name="planId" th:value="${plan.id}">
                            <input  type="hidden" name="orderNum" th:value="${makeSheet.sheetType == 1? plan.orderNum : plan.resultOrderNum}">
                            <input  type="hidden" name="pid" th:value="${makeSheet.sheetType == 1? plan.pid : plan.pid}">
                            <input style="text-align:left;" type="text" class="form-control" name="afterDrawNo" th:value="${makeSheet.sheetType == 1 ? plan.drawNo : plan.afterDrawNo}" th:readonly="${plan.changeStatus==1}" autocomplete="off">
                        </td>
                        <td colspan="4">
                            <input type="hidden" name="technologyChange" value="-2">
                            <input type="text" class="form-control" name="afterDrawName" th:value="${makeSheet.sheetType == 1 ? plan.drawName : plan.afterDrawName}" th:readonly="${plan.changeStatus==1}" autocomplete="off">
                        </td>
                        <td>
                            <input type="number" step="0.1" class="form-control" name="afterDrawNum" th:value="${makeSheet.sheetType == 1 ? plan.drawNum : plan.afterDrawNum}" th:readonly="${plan.changeStatus==1}" autocomplete="off">
                        </td>
                        <td colspan="2">
                            <select class="form-control" name="afterMakeType" th:disabled="${plan.changeStatus==1}">
                                <option value="">请选择</option>
                                <option th:each="p : ${makeTypes}" th:value="${p.name}"
                                        th:text="${p.name}" th:selected="${makeSheet.sheetType ==1 ? p.name==plan.makeType : p.name == plan.afterMakeType}"></option>
                            </select>
                        </td>
                        <td colspan="2">
                            <input type="hidden" name="afterUrl">
                            <input type="text" class="form-control" name="afterRemark" th:value="${makeSheet.sheetType == 1 ? plan.remark : plan.afterRemark}" th:readonly="${plan.changeStatus==1}" autocomplete="off">
                        </td>
                        <td>
                            <span th:if="${plan.changeStatus!=1}" class="out-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                            <span th:if="${plan.changeStatus!=1}" class="out-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div class="col-xs-10 col-xs-offset-1" style="font-size: 14px;line-height: 200%;">
                <div class="col-xs-3" style="padding-left: 0">提报人： [[${session.user.name}]]</div>
                <div class="col-xs-3 bottom form-inline">联系电话：
                    <input class="form-control" style="width:60%;border: none;border-bottom: 1px solid #000;" type="text" name="postTel" th:value="${makeSheet.postTel}">
                </div>
            </div>
        </form>
    </div>
    <div id="div-makeType" style="display: none">
        <select class="form-control" name="afterMakeType">
            <option value="">请选择</option>
            <option th:each="makeType : ${makeTypes}" th:value="${makeType.name}"
                    th:text="${makeType.name}"></option>
        </select>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:src="@{/plugin/SheetJs/xlsx.core.min.js}"></script>
<script th:inline="javascript">
    let tempUrl,trAutoIndex=9999,dynamicId=0,orderNum,pidNum;

    /** 计算表格序号 */
    function computerNo() {
        $("table tbody tr").each(function (i, e) {
            $(this).children("td:first").text(i + 1);
        });
    }

    $(function () {
        if([[${isChange}]]==-1){
            js.modal.success("此项目已有变更在审核流程中，请审核通过后再对此项目进行变更！")
        }


        let makeTypeHtml = $("#div-makeType").html();
        /** 添加一行 */
        $("#sheetForm").on('click', '.out-add', function () {
            /** 动态添加《td》的记录数*/
            trAutoIndex++;
            /** 动态添加下拉列表的记录数*/
            dynamicId++;
            /** 添加没行数据时获得当前行数下的orderNum*/
            orderNum = $(this).parents("tr").attr("orderNumId")
            pidNum = $(this).parents("tr").attr("pidNumId")
            $(this).parents("tr").after(`
                    <tr trtype='cloneTR' teid='`+trAutoIndex+`' orderNumId='`+orderNum+`' pidNumId='`+pidNum+`'>
                        <td afterTdOrdernum="afterTdOrdernum"></td>
                        <input type="hidden" name="planId" value="0">
                        <input type="hidden" name="afterUrl">
                        <td colspan="4">
                            <input style="text-align:left;" type="text" class="form-control" name="afterDrawNo" autocomplete="off" >
                        </td>
                         <td colspan="4">
                            <input type="text" class="form-control" name="afterDrawName" autocomplete="off">
                             <input type="hidden" name="orderNum" value='`+orderNum+`'>
                             <input  type="hidden" name="pid" value='`+pidNum+`'>
                        </td>
                        <td>
                            <input type="number" step="0.1" class="form-control" name="afterDrawNum" autocomplete="off">
                        </td>
                        <td colspan="2">
                            <input type="hidden" name="technologyChange" value="-2">
                            ${makeTypeHtml}
                        </td>
                        <td colspan="2">
                            <input type="text" class="form-control" name="afterRemark" autocomplete="off"  >
                        </td>
                        <td>
                            <span class="out-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                            <span class="out-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                        </td>
                    </tr>
            `);
            computerNo();
        });


        /** 删除行 */
        $('#sheetForm').on('click', '.out-del', function () {
            let $this = $(this);
            let id = $(this).parents("tr").find("input[name=planId]").val();
            var aa=$(this).parents("tr").attr("teid");
            if (id!=0) {

                /** 删除时的状态2为变更删除*/
                $this.parents("tr").find("input[name=technologyChange]").attr("value",2);
                $this.parents("tr").find("input[name=afterDrawNo]").attr({"readonly":true,"style":"color:red"});
                $this.parents("tr").find("input[name=afterDrawName]").attr({"readonly":true,"style":"color:red"});
                $this.parents("tr").find("input[name=afterDrawNum]").attr({"readonly":true,"style":"color:red"});
                $this.parents("tr").find("select[name=afterMakeType]").attr({"disabled":"disabled","style":"color:red"});
                $this.parents("tr").find("input[name=afterRemark]").attr({"readonly":true,"style":"color:red"});
            } else {
                $this.parents("tr").remove();
                computerNo();
            }

        });

        $("#sheetForm").on('click', '.out-upload', function () {
            tempUrl = $(this).parents("tr").find("input[name=afterUrl]");
            js.modal.open('modal-upload');
        });
        /** 初始化验证 */
        js.validate.init("uploadForm", {
            fields: {
                file: {
                    validators: {
                        notEmpty: {message: '上传文件不能为空'}
                    }
                }
            }
        });
        /** 上传附件 */
        $("#btn-upload").click(function () {
            var $this = $(this);
            $this.attr('disabled', true);
            js.validSubmit({
                formId: "uploadForm",
                url: ctx + "report/list/out/change/save/upload",
                data: new FormData($("#uploadForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.alert(result.msg);
                        tempUrl.val(result.data);
                        js.modal.hide("modal-upload");
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
            $this.attr('disabled', false);
        });
        /** 重置表单及验证 */
        js.modal.hideEvent("modal-upload", function () {
            js.validate.reset("uploadForm");
            js.reset("uploadForm");
        });

        /** 保存计划单 */
        $("#btn-save").click(function () {
            let $this = $(this);
            $("table tbody tr").find("select[name=afterMakeType]").attr({"disabled":false,"readonly":true})
            let res = js.vd({
                id: 'sheetForm',
                notEmpty: ['proCode', 'afterDrawNo', 'afterDrawName', 'afterDrawNum','afterMakeType', 'changeReason', 'postTel']
            })
            if (!res) {
                return;
            }
            $this.attr('disabled', true);
            js.submit({
                formId: "sheetForm",
                url: ctx + 'production/list/make/change/modifySave',
                data: new FormData($("#sheetForm")[0]),
                success: function (result) {
                    ;
                    if (result.type === web_status.SUCCESS) {
                        location.href = ctx + "production/list/modifyChangeEdit/"+result.data;
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
            $this.removeAttr('disabled');
        });

    });
</script>
</body>
</html>
